package busstopfinder.ui;

import android.app.Activity;
import android.bustopfinder.R;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class BusLineActivity extends Activity {
	
	private static String TAG = "BSF";

	private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    
    private GestureDetector gestureDetector;
	View.OnTouchListener gestureListener;
	
    
    FlingScrollView mTopView;

    
    // TODO: remove defaults
    private int numScreens = 1;
    private int[] lines = {12};

    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bus_line);
        
        // Set lines and numScreens from extra
        this.lines = this.getIntent().getIntArrayExtra("lines");
        this.numScreens = lines.length;

        
        // Capture top view
        mTopView = (FlingScrollView) this.findViewById(R.id.topView);
        mTopView.setInitialPosition(0);
        
        // Now, insert views in top view. Nesting:
        // Fling Scoll View (captured)
        // - ScrollView (created here)
        // -- FrameLayout (created here)
        // ---ImageView navigation (created here)
        // -- LinearLayout (created here)
        // --- ImageView logo (created here)
        // --- ImageView stops (created here)
        for (int i = 0; i < numScreens; i++) {
        	
        	ScrollView mScrollV = new ScrollView(this);
        	ViewGroup.LayoutParams mScrollVParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
        			ViewGroup.LayoutParams.FILL_PARENT);
        	mScrollV.setLayoutParams(mScrollVParams);
        	mTopView.addView(mScrollV);
        	
        	
        	
        	
        	LinearLayout mLinLayout = new LinearLayout(this);
        	LinearLayout.LayoutParams mLinLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 
        			LinearLayout.LayoutParams.FILL_PARENT);
        	mLinLayout.setLayoutParams(mLinLayoutParams);
        	mLinLayout.setOrientation(LinearLayout.VERTICAL);
        	mScrollV.addView(mLinLayout);
        	
        	FrameLayout mFrameLayout = new FrameLayout(this);
        	FrameLayout.LayoutParams mFrameParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT, Gravity.TOP);
        	mFrameLayout.setLayoutParams(mFrameParams);
        	mLinLayout.addView(mFrameLayout);
        	
        	// Dots
        	ImageView mImgDots = new ImageView(this);
        	mImgDots.setImageBitmap(getBitmap(i+1));
        	mImgDots.setPadding(0, 10, 0, 0);
        	LinearLayout.LayoutParams mImgDotsParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 
        			LinearLayout.LayoutParams.WRAP_CONTENT);
        	mImgDots.setLayoutParams(mImgDotsParams);
        	mFrameLayout.addView(mImgDots);
        	
        	
        	
        	// Logo
        	int mIdLogo = getResources().getIdentifier("logo_" + lines[i], "drawable", getPackageName());
        	ImageView mImgLogo = new ImageView(this);
        	mImgLogo.setImageResource(mIdLogo);
        	mImgLogo.setPadding(0, 20, 0, 0);
        	LinearLayout.LayoutParams mImgLogoParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 
        			LinearLayout.LayoutParams.WRAP_CONTENT);
        	mImgLogo.setLayoutParams(mImgLogoParams);
        	mLinLayout.addView(mImgLogo);
        	
        	// Line
        	int mIdStops = getResources().getIdentifier("stops_" + lines[i], "drawable", getPackageName());
        	ImageView mImgLine = new ImageView(this);
        	mImgLine.setImageResource(mIdStops);
        	mImgLine.setPadding(0, 20, 0, 0);
        	LinearLayout.LayoutParams mImgLogoLine = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 
        			LinearLayout.LayoutParams.WRAP_CONTENT);
        	mImgLine.setLayoutParams(mImgLogoParams);
        	mImgLine.setAdjustViewBounds(true);
        	mLinLayout.addView(mImgLine);
        }
        

        
        // Star gesture detector
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
    }
      
    private Bitmap getBitmap (int numDots) {
    	Bitmap ret = null;
    	switch (numScreens) {
    	// just one screen: return null (blank image)
    	case 1:
    		break;
    	// 2 screens
    	case 2:
    		
    		switch (numDots) {
    		case 1:
    			ret = BitmapFactory.decodeResource(getResources(), R.drawable.dots_1_2);
    			break;
    		case 2:
    			ret = BitmapFactory.decodeResource(getResources(), R.drawable.dots_2_2);
    			break;
    		}
    		
    		break;
    	
    	case 3:
    		
    		switch (numDots) {
    		case 1:
    			ret = BitmapFactory.decodeResource(getResources(), R.drawable.dots_1_3);
    			break;
    		case 2:
    			ret = BitmapFactory.decodeResource(getResources(), R.drawable.dots_2_3);
    			break;
    		case 3:
    			ret = BitmapFactory.decodeResource(getResources(), R.drawable.dots_3_3);
    			break;
      		}
    		
    		break;
    	
    	case 4:

    		switch (numDots) {
    		case 1:
    			ret = BitmapFactory.decodeResource(getResources(), R.drawable.dots_1_4);
    			break;
    		case 2:
    			ret = BitmapFactory.decodeResource(getResources(), R.drawable.dots_2_4);
    			break;
    		case 3:
    			ret = BitmapFactory.decodeResource(getResources(), R.drawable.dots_3_4);
    			break;
    		case 4:
    			ret = BitmapFactory.decodeResource(getResources(), R.drawable.dots_4_4);
    			break;
      		}
    		
    		break;
    	
    	}
    	return ret;
    }
    
    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        	Log.w(TAG, "inside onFling");
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                	Log.w(TAG, "right.left");
 
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                	Log.w(TAG, "left.right");
 

                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
	        return true;
	    else
	    	return false;
    }
    
    
}